#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import time
import datetime
import winsound
from threading import Thread
import win32api,win32con


class AlarmClock(Thread):
    c = 'r'
    count_down = 0
    mode = None
    count = 0

    def __init__(self):
        super(AlarmClock, self).__init__()

    def setCountDown(self, count_down):
        self.count_down = count_down

    def setWakeUpMode(self, mode):
        self.mode = mode

    def setState(self, c):
        self.c = c

    def done(self):
        if self.mode:
            if self.mode == "y":
                win32api.keybd_event(0x11,0,0,0)  # Ctrl键
                win32api.keybd_event(0x12,0,0,0)  # Alt键
                win32api.keybd_event(0x50,0,0,0)  # p键

                # 释放按键`
                win32api.keybd_event(0x50,0,win32con.KEYEVENTF_KEYUP,0)
                win32api.keybd_event(0x12,0,win32con.KEYEVENTF_KEYUP,0)
                win32api.keybd_event(0x11,0,win32con.KEYEVENTF_KEYUP,0)
                return
            elif self.mode == "f":
                win32api.keybd_event(0x11,0,0,0)  # Ctrl键
                win32api.keybd_event(0x10,0,0,0)  # Shift键
                win32api.keybd_event(0x50,0,0,0)  # p键

                # 释放按键
                win32api.keybd_event(0x50,0,win32con.KEYEVENTF_KEYUP,0)
                win32api.keybd_event(0x10,0,win32con.KEYEVENTF_KEYUP,0)
                win32api.keybd_event(0x11,0,win32con.KEYEVENTF_KEYUP,0)
                return

        sound = "欢快节奏韵律音效.wav"
        winsound.PlaySound(sound, winsound.SND_FILENAME|winsound.SND_NOWAIT )

    def run(self):
        while True:
            if self.c == 'i':
                time.sleep(1)
                continue
            if self.c == 'c' or self.c == 'e':
                return

            left = self.count_down - self.count
            if left >= 0:
                hours,rem = divmod(left, 3600)
                minutes,seconds = divmod(rem, 60)
                print("\r [Left]  %02d:%02d:%02d" % (hours,minutes,seconds), end='')
            else:
                break

            time.sleep(1)
            self.count = self.count + 1

        self.done()
